-- @owner: opentestcase023
-- @date: 2022/02/24
-- @testpoint: 当向src视图中插入数据时，将插入数据同时插入到des表中，删除src视图时，删除des表中id1=1的行 each row

--step1:建表;expect:成功
drop table if exists t_trigger_src_00067 cascade;
create table t_trigger_src_00067(id1 int, id2 int, id3 int) with (storage_type=ustore);

--step2:当向src视图中插入数据时，将插入数据同时插入到des表中，删除src视图时，删除des表中id1=1的行 each row;expect:成功
create or replace view t_trigger_src_00067_view1 as select * from t_trigger_src_00067;
create or replace function insert_func()
returns trigger as $$
begin
if(tg_op='DELETE')
then
delete from t_trigger_src_00067 where id1=1;
return new;
end if;
if(tg_op='INSERT')
then
insert Into t_trigger_src_00067 values(new.id1,new.id2,new.id3);
return new;
end if;
return null;
end;
$$language plpgsql;
/
drop trigger if exists insert_trigger_1 on t_trigger_src_00067_view1;
create trigger insert_trigger_1
instead of delete or insert on t_trigger_src_00067_view1
for each row
execute procedure insert_func();
/
insert into t_trigger_src_00067 values(1,1,1);
select * from t_trigger_src_00067 order by id1;
insert into t_trigger_src_00067_view1 values(1,1,1);
select * from t_trigger_src_00067 order by id1;
delete from t_trigger_src_00067_view1 where id1=2;
select * from t_trigger_src_00067 order by id1;

--step3:清理环境;expect:成功
drop trigger if exists insert_trigger_1 on t_trigger_src_00067_view1;
drop table if exists t_trigger_src_00067 cascade;
drop view if exists t_trigger_src_00067_view1;
drop function if exists insert_func;